//index.js
//获取应用实例
let app = getApp()
let formatTime = require('../../utils/util.js')
Page({
  data: {
    validCoupons: [],
    invalidCoupons: [],
    usedCoupons: [],
    showCoupons: [],
    statusType: ["未使用", "使用记录"],
    currentType: 0,
    couponsNum: [0, 0]
  },
  onLoad: function () {
    this.getMyCoupons()
  },

  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index;
    this.data.currentType = curType
    this.setData({
      currentType: curType
    });


    this.setData({
      showCoupons: curType == 0 ? this.data.validCoupons : this.data.usedCoupons
    });
    this.onShow();
  },

  onShow : function () {
    //this.getMyCoupons()
  },
 
  getMore:function(){
    wx.navigateTo({
      url: '/pages/get-coupons/index'
    })
  },

  /**
   * 查看我的优惠券
   */
  getMyCoupons: function () {
    let token = wx.getStorageSync('token')
    let that = this
    wx.request({
      url: app.apiUrl('/market/coupon/usercoupon'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {

      },
      success: function (res) {
        console.log('getMyCoupons')
        console.log(res.data)
        if (res.data.code >= app.globalData.successCode) {

          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else {
          let couponsNumTemp = that.data.couponsNum
          couponsNumTemp[0] = res.data.data.unused_length
          couponsNumTemp[1] = res.data.data.used_length
          

          for (var i = 0; i < couponsNumTemp[0]; i++) {
            res.data.data.coupon_unused_list[i].status = 0
            // let dateEnd = formatTime.formatTime(new Date(formatTime.getDateDiff(res.data.data.coupon_unused_list[i].end_at)))
            // let dateStart = formatTime.formatTime(new Date(res.data.data.coupon_unused_list[i].start_at)) 
            res.data.data.coupon_unused_list[i].date_from = formatTime.formatDatePoint(new Date(res.data.data.coupon_unused_list[i].start_at)) + '-' + formatTime.formatDatePoint(new Date(res.data.data.coupon_unused_list[i].end_at))
            res.data.data.coupon_unused_list[i].click = false
            res.data.data.coupon_unused_list[i].value = res.data.data.coupon_unused_list[i].type == 1 ? res.data.data.coupon_unused_list[i].value * 10 : res.data.data.coupon_unused_list[i].value

          }

          for (var i = 0; i < couponsNumTemp[1]; i++) {
            res.data.data.coupon_used_list[i].status = 1
            // let dateEnd = formatTime.formatTime(new Date(res.data.data.coupon_used_list[i].end_at))
            // let dateStart = formatTime.formatTime(new Date(res.data.data.coupon_used_list[i].start_at))
            res.data.data.coupon_used_list[i].date_from = formatTime.formatDatePoint(new Date(res.data.data.coupon_used_list[i].start_at)) + '-' + formatTime.formatDatePoint(new Date(res.data.data.coupon_used_list[i].end_at))
            res.data.data.coupon_used_list[i].click = false
            res.data.data.coupon_used_list[i].value = res.data.data.coupon_used_list[i].type == 1 ? res.data.data.coupon_used_list[i].value * 10 : res.data.data.coupon_used_list[i].value
          }

          that.setData({
            coupons: res.data.data,
            validCoupons: res.data.data.coupon_unused_list,
            usedCoupons: res.data.data.coupon_used_list,
            showCoupons: res.data.data.coupon_unused_list,
            couponsNum: couponsNumTemp
          })
        }
        
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },

  getDetail: function (e) {
    let index = e.currentTarget.dataset.index
    let coupons = this.data.showCoupons
    coupons[index].click = coupons[index].click? false : true
    this.setData({
      showCoupons: coupons
    })
  },

  filter: function (coupons = [], type) {
    let couponsTemp = []
    for (var i = 0; i < coupons.length; i++) {
      if (coupons[i].status == type)
        couponsTemp.push(coupons[i]) 
    }
    return couponsTemp
  },

  toUse: function () {
    wx.switchTab({
      url: "/pages/index/index"
    })
  }

})
